package cc.realxyq.demo;

import cc.realxyq.demo.bean.Person;
import cc.realxyq.demo.bean.Teacher;
import cc.realxyq.demo.config.ComponentScanConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

/**
 * @author kwhan
 * @date 2022年01月07日 4:46
 */
@Slf4j
public class DemoApplication {
    public static void main(String[] args) {

        // 通过读取xml文件，初始化bean实例
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        Object teacher = applicationContext.getBean("teacher");
        log.warn("通过配置文件加载的teacher对象：{}",teacher);
        Arrays.stream(applicationContext.getBeanNamesForType(Teacher.class)).forEach(log::warn);

        // 通过javaConfig的方式初始化bean
        ApplicationContext context = new AnnotationConfigApplicationContext(ComponentScanConfig.class);
        Arrays.stream(context.getBeanNamesForType(Person.class)).forEach(log::info);
        Person person = context.getBean(Person.class);
        log.info("Person对象创建了：{}",person);
    }
}
